package netty.selector;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author cyt
 * @create 2020-11-04 17:43
 */
public class TestClient {
    public static void main(String[] args) throws IOException {
        /**
         * 得到一个网络的通道， 并且设置为非阻塞
         */
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6767);
        /**
         * 开始连接， 并且连接成功
         */
        if (!socketChannel.connect(inetSocketAddress)) {
            while (!socketChannel.finishConnect()) {
                System.out.println("连接需要时间， 客户端不会阻塞， 可以做其他工作- >> ");
            }
        }
        /**
         * 写入到 通道中
         */
        String str = "hello abstractclass.test success";
        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
        socketChannel.write(buffer);
        System.in.read();
    }
}
